import router from '@/router/index.js'
import { ElMessage } from 'element-plus'
import { me } from '@/api/system/user.js'

const state = () => ({
  user: {
    id: '',
    username: '',
    tel: null,
    emial: null,
    password: null,
    icon: null,
    nickname: '',
    gender: '',
    age: null,
    type: null,
    status: null,
    remark: null,
    usernameUpdateTime: null,
    loginLocation: '',
    ip: '',
    os: '',
    loginTime: null
  }
})

const mutations = {
  SET_USER: (state, user) => {
    state.user = user
  },
  SET_PERMISSIONS: (state, permissions) => {
    state.permissions = permissions
  },
  REMOVE_USER: (state) => {
    state.user = {}
    sessionStorage.removeItem('authorization')
  }
}

const actions = {
  async getMe({ commit }) {
    const res = await me()
    if (res) {
      if (res && res.code === 200) {
        commit('SET_USER', res.data)
      }
    } else {
      ElMessage({
        message: '获取用户信息失败',
        grouping: true,
        type: 'error',
        customClass: 'custom-message-style'
      })
    }
  },
}

export default {
  namespaced: true, // 命名空间
  state,
  mutations,
  actions
}